/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cn.ekuma.epos.datalogic.define.dao.erp;

import java.util.List;
import cn.ekuma.data.dao.ModifiedLogDAO;
import cn.ekuma.epos.db.table.erp.I_SharedOrder;

import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.DataParams;
import com.openbravo.data.loader.DataRead;
import com.openbravo.data.loader.DataWrite;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.ImageUtils;
import com.openbravo.data.loader.PreparedSentence;
import com.openbravo.data.loader.SerializerReadBasic;
import com.openbravo.data.loader.SerializerReadString;
import com.openbravo.data.loader.SerializerWriteParams;
import com.openbravo.data.loader.SerializerWriteString;
import com.openbravo.data.loader.StaticSentence;
import com.openbravo.data.loader.TableDefinition;
import com.openbravo.bean.erp.Order;
import com.openbravo.bean.erp.SharedOrder;
import com.openbravo.data.loader.Datas;
import com.openbravo.data.model.Field;
import com.openbravo.format.Formats;

/**
 *
 * @author Administrator
 */
public class SharedOrderDAO  extends ModifiedLogDAO<SharedOrder>{

    public SharedOrderDAO(I_Session s) {
        super(s);
    }

    @Override
    public TableDefinition getTable() {
        return new TableDefinition(s,
                  "SHAREDORDERS",
                  new Field[]{
                       new Field(I_SharedOrder.ID,Datas.STRING,Formats.STRING),
                       new Field(I_SharedOrder.CONTENT,Datas.BYTES,Formats.BYTEA),
                       new Field(I_SharedOrder.LOCKED,Datas.BOOLEAN,Formats.BOOLEAN),
                       new Field(I_SharedOrder.DEFINETYPEREF,Datas.INT,Formats.INT),
                       new Field(I_SharedOrder.ORDERTYPE,Datas.INT,Formats.INT),
                       
                       new Field(I_SharedOrder.ORDERID,Datas.STRING,Formats.STRING),
                       new Field(I_SharedOrder.CURDATE,Datas.TIMESTAMP,Formats.TIMESTAMP),
                       new Field(I_SharedOrder.PERSON,Datas.STRING,Formats.STRING),
                       new Field(I_SharedOrder.CUSTOMER,Datas.STRING,Formats.STRING),
                       new Field(I_SharedOrder.PRICERATE,Datas.DOUBLE,Formats.DOUBLE),
                       
                       new Field(I_SharedOrder.ATTRIBUTESETINSTANCE_ID,Datas.STRING,Formats.STRING),
                       new Field(I_SharedOrder.TOTALPRICE,Datas.DOUBLE,Formats.DOUBLE),
                       new Field(I_SharedOrder.LOCATIONREF,Datas.STRING,Formats.STRING),
                       new Field(I_SharedOrder.LOCATIONREF1,Datas.STRING,Formats.STRING),
                       new Field(I_SharedOrder.MEMO,Datas.STRING,Formats.STRING),
                       
                       new Field(I_SharedOrder.REFERENCEPERSON,Datas.STRING,Formats.STRING),
                       new Field(I_SharedOrder.STATE,Datas.INT,Formats.INT),
                       new Field(I_SharedOrder.LASTMODIFIED,Datas.TIMESTAMP,Formats.TIMESTAMP),
                       new Field(I_SharedOrder.LOCKUSERID,Datas.TIMESTAMP,Formats.TIMESTAMP),
                       new Field(I_SharedOrder.REFDATE,Datas.TIMESTAMP,Formats.TIMESTAMP)
                      },
                  new int[]{0});
    }

    @Override
    public void writeInsertValues(DataWrite dp, SharedOrder obj) throws BasicException {
    	Order order=obj.getOrder();
    	dp.setString(1, obj.getId());
    	dp.setBytes(2, ImageUtils.writeSerializable(order));
    	dp.setBoolean(3, obj.isLocked());
    	dp.setString(4, order.getDefineType().getId());
    	dp.setInt(5, order.getOrderType());
    	
    	dp.setString(6, order.getOrderId());
    	dp.setTimestamp(7, order.getDate());
    	dp.setString(8, order.getUser().getId());
    	dp.setString(9,order.getCustomer()==null?null:order.getCustomer().getId());
    	dp.setDouble(10, order.getM_dPriceRate());
    	
    	dp.setString(11, order.getAttsetinstid());
    	dp.setDouble(12, order.getTotalPrice());
    	dp.setString(13, order.getLocationRef());
    	dp.setString(14, order.getLocationRef1());
    	dp.setString(15, order.getMemo());
    	
         if(order.getReference_User()!=null)
        	 dp.setString(16,order.getReference_User().getId());
        else
        	dp.setString(16,null);
        dp.setString(17,order.getCurrentState());
        dp.setTimestamp(18, obj.getLastModified());
        dp.setString(19,obj.getLockUserId());
        dp.setTimestamp(20,order.getM_dRefDate());
    }

    public SharedOrder readValues(DataRead dr) throws BasicException {
       throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Class getSuportClass() {
        return SharedOrder.class;
    }
    
    
    public Order getSharedOrder(String Id) throws BasicException {
        if (Id == null) {
            return null;
        } else {
            Object[]record = (Object[]) new StaticSentence(s
                    , "SELECT CONTENT FROM SHAREDORDERS WHERE ID = ?"
                    , SerializerWriteString.INSTANCE
                    , new SerializerReadBasic(new Datas[] {Datas.SERIALIZABLE})).find(Id);
            return record == null ? null : (Order) record[0];
        }
    }
    
    
    public List<String> getSharedOrderList() throws BasicException {
        return (List<String>) new StaticSentence(s
                , "SELECT ID FROM SHAREDORDERS WHERE LOCKED="+s.getDB().FALSE()+" ORDER BY ID"
                , null
                , SerializerReadString.INSTANCE).list();
    }
    
    public void setSharedOrderLocked(final String id, final boolean locked, final String userId) throws BasicException {
        Object[] values = new Object[] {id};
        Datas[] datas = new Datas[] {Datas.STRING};
        new PreparedSentence(s
                , "UPDATE SHAREDORDERS SET LOCKED="+(locked==true?s.getDB().TRUE():s.getDB().FALSE())+" ,LOCKUSERID= ? "+"  WHERE ID = ?"
                ,SerializerWriteParams.INSTANCE ).exec(new DataParams() {
                    public void writeValues() throws BasicException {
                    	setString(1, userId);
                    	setString(2,id);
                    }});
    }
    
    public void unLockOrderByUserID(String userId) throws BasicException {
    	new StaticSentence(s
                , "UPDATE SHAREDORDERS SET LOCKED="+s.getDB().FALSE()+" WHERE LOCKED="+s.getDB().TRUE()+" AND LOCKUSERID=?"
                , SerializerWriteString.INSTANCE
                , null).exec(userId);
    }

	@Override
	public SharedOrder readValues(DataRead dr, SharedOrder obj)
			throws BasicException {
		// TODO Auto-generated method stub
		return null;
	}
}
